<?PHP
  // Contains commonly used functions.
 
  session_start();

  // User type selection variables
  $choose_one_select = htmlspecialchars("");
  $choose_one_select1 = htmlspecialchars("");
  $choose_one_select2 = htmlspecialchars("");
  $a_selection = htmlspecialchars("");
  $v_selection = htmlspecialchars("");
  $s_selection = htmlspecialchars("");
  $m_selection = htmlspecialchars("");
  $f_selection = htmlspecialchars("");
  $yp_selection = htmlspecialchars("");
  $np_selection = htmlspecialchars("");

  //********************************************************************
  // Run the SQL, and return the error flag and the cursor in an array.
  // The array index "flag" contains the flag.
  // The array index "cursor" contains the cursor.
  //********************************************************************
  function execute_sql_in_oracle($sql) {

    $connection = oci_connect("vluong","password");

    if(!$connection) {

      // Failed to connect
      display_oracle_error_message(null);
      die("Failed to connect.");
    }
  
    $cursor = oci_parse($connection, $sql);

    if (!$cursor) {

      display_oracle_error_message($connection);
      oci_close($connection);

      // SQL failed 
      die("SQL parsing failed.");
    }

    // Execute SQL query
    $result = oci_execute($cursor);

    // Close the connection with oracle
    oci_close($connection);  

    $return_array["flag"] = $result;
    $return_array["cursor"] = $cursor;

    return $return_array;
  }

  //**************************************************************
  // Verify user page access privilege
  //**************************************************************
  function verify_privileges($page_type) {

    if ((empty($_SESSION['aflag']) or $_SESSION['aflag'] == "n") and $page_type == "A") {
      // Unauthorized access
      header("Location:error_page.php");
    }
    if ((empty($_SESSION['sflag']) or $_SESSION['sflag'] == "n") and $page_type == "S"
         and (empty($_SESSION['aflag']) or $_SESSION['aflag'] == "n")) {
      // Unauthorized access
      header("Location:error_page.php");
    }
  } 

  //**************************************************************
  // Use to reset form fields
  //**************************************************************
  function resetForm() {

    $_SESSION['input_user_id'] = "";
    $_SESSION['input_password'] = "";
    $_SESSION['input_confirm_password'] = "";
    $_SESSION['input_fname'] = "";
    $_SESSION['input_lname'] = "";
    $_SESSION['input_dob'] = "";
    $_SESSION['input_email'] = "";
    $_SESSION['input_confirm_email'] = "";
    $_SESSION['input_street'] = "";
    $_SESSION['input_city'] = "";
    $_SESSION['input_state'] = "";
    $_SESSION['input_zip'] = "";
    $_SESSION['input_phone'] = "";
    $_SESSION['input_gender'] = "";
    $_SESSION['input_promo'] = "";
    $_SESSION['input_time'] = "";
    $_SESSION['input_day'] = "";
    $_SESSION['input_skill'] = "";
  }

  //***************************************************
  // Password strength validation
  // Must be at least 8 characters long and 
  // contain at least 1 uppercase, 1 lowercase, 
  // 1 special character, and 1 number.
  //***************************************************
  function validiatePassword($candidate) {

    $r1='/[A-Z]/';  // Uppercase
    $r2='/[a-z]/';  // Lowercase
    $r3='/[!@#$%^&*()-_=+{};:,<.>]/';  // Special chars
    $r4='/[0-9]/';  // Numbers

    if(preg_match_all($r1,$candidate, $o)<1) return FALSE;

    if(preg_match_all($r2,$candidate, $o)<1) return FALSE;

    if(preg_match_all($r3,$candidate, $o)<1) return FALSE;

    if(preg_match_all($r4,$candidate, $o)<1) return FALSE;

    if(strlen($candidate)<8) return FALSE; 

    return TRUE;
  }

  //***************************************************
  // E-mail validation
  //***************************************************
  function validateEmail($email){

    return preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^", $email);
  }

  //***************************************************
  // Use to format the address display on 
  // User Management Main using the appropriate 
  // database fields
  //***************************************************
  function &formatAddress($street, $city, $state, $zip) {
    $address = "";
    if (!empty($street) and !empty($city) and !empty($state))
      $address = $street . "<br>".$city . ", ".$state . " ".$zip;
    else if (!empty($street) and !empty($city)) 
      $address = $street . "<br>".$city;
    else if (!empty($street) and empty($city))
      $address = $street;
    else
      $address = "<br>".$state . " ".$zip;
    return $address;
  }

  //***************************************************
  // Takes an executed errored oracle cursor as input.
  // Display an interpreted error message.
  //***************************************************
  function display_oracle_error_message($resource) {

    if (is_null($resource))
      $err = oci_error();
    else
      $err = oci_error($resource);

    echo "<BR />";
    echo "Oracle Error Code: " . $err['code'] . "<BR />";
    echo "Oracle Error Message: " . $err['message'] . "<BR />" . "<BR />";
  
    if ($err['code'] == 1)
      echo("Duplicate Values.  <BR /><BR />");
    else if ($err['code'] == 984 or $err['code'] == 1861 
      or $err['code'] == 1830 or $err['code'] == 1839 or $err['code'] == 1847
      or $err['code'] == 1858 or $err['code'] == 1841)
      echo("Wrong type of value entered.  <BR /><BR />");
    else if ($err['code'] == 1400 or $err['code'] == 1407)
      echo("Required field not correctly filled.  <BR /><BR />");
    else if ($err['code'] == 2292)
      echo("Child records exist.  Need to delete or update them first.  <BR /><BR />");
  }
?>